package com.youku.plugins.controlbarV4.view.components
{
   import flash.display.Sprite;
   import com.youku.baseUI.SimpleVideoPlayer;
   import flash.text.TextField;
   import flash.utils.Timer;
   import flash.display.MovieClip;
   import com.youku.plugins.controlbarV4.ControlBarConstant;
   import flash.text.TextFormat;
   import flash.text.TextFieldAutoSize;
   import flash.text.TextFormatAlign;
   import flash.events.MouseEvent;
   import flash.events.TimerEvent;
   import com.youku.plugins.controlbarV4.view.events.PreviewerEvent;
   import com.youku.utils.ReportUtil;
   import com.youku.utils.Tweener;
   import flash.utils.setTimeout;
   import flash.display.DisplayObject;
   import com.youku.events.CommonPlayerEvent;
   import com.youku.utils.AssetUtil;
   import flash.filters.DropShadowFilter;
   
   public class Previewer extends Sprite
   {
      
      public function Previewer() {
         super();
         this.init();
      }
      
      private static const PADDING:Number = 7;
      
      private static const VIDEO_WIDTH:Number = 128;
      
      private static const VIDEO_HEIGHT:Number = 72;
      
      private static const TEXT_WIDTH_MAX:Number = 126;
      
      private static const PREVIEW_REPLAY_TIMEOUT:Number = 3 * 1000;
      
      private static const PREVIEW_DISAPPEAR_TIMEOUT:Number = 3 * 1000;
      
      protected var _background:Sprite;
      
      protected var _video:SimpleVideoPlayer;
      
      protected var _errorText:TextField;
      
      protected var _timeText:TextField;
      
      protected var _arrow:Sprite;
      
      protected var _container:Sprite;
      
      protected var _imageDisplayer:PreviewEffectDisplayer;
      
      protected var _replayTimer:Timer = null;
      
      private var _playerWidth:Number = 0;
      
      private var _videoError:Boolean = false;
      
      private var _textLength:Number = -1;
      
      private var _isShowPreviewVideo:Boolean;
      
      private var _tweenSpeed:Number = 0.15;
      
      private var _BGWidth:Number;
      
      private var _delayVideoTimer:Timer;
      
      private var _videoURL:String;
      
      private var _isImagePreview:Boolean;
      
      public var previewMode:String;
      
      private var _jumpTime:Number = -1;
      
      private var _imagePreviewLayer:Sprite;
      
      private var _imageBorder:MovieClip;
      
      private var _imageTF:TextField;
      
      private function init() : void {
         this._container = new Sprite();
         addChild(this._container);
         this._background = new Sprite();
         this._background.graphics.beginFill(ControlBarConstant.TIPS_BG_COLOR,ControlBarConstant.TIPS_BG_ALPHA);
         this._background.graphics.drawRect(-100,-200,200,200);
         this._background.graphics.endFill();
         this._background.width = 40;
         this._background.height = 20;
         this._video = new SimpleVideoPlayer();
         this._errorText = new TextField();
         this._errorText.defaultTextFormat = new TextFormat("Arial",12,16777215);
         this._errorText.autoSize = TextFieldAutoSize.CENTER;
         this._errorText.width = 100;
         this._errorText.height = 20;
         this._errorText.selectable = false;
         this._errorText.text = "预览加载失败";
         var _loc1_:TextFormat = new TextFormat();
         _loc1_.font = "Arial";
         _loc1_.size = 12;
         _loc1_.color = 16777215;
         _loc1_.leftMargin = 5;
         _loc1_.rightMargin = 5;
         _loc1_.leading = 1;
         _loc1_.align = TextFormatAlign.JUSTIFY;
         this._timeText = new TextField();
         this._timeText.defaultTextFormat = _loc1_;
         this._timeText.selectable = false;
         this._timeText.multiline = true;
         this._timeText.wordWrap = true;
         this._timeText.cacheAsBitmap = true;
         this._timeText.width = 80;
         this._timeText.height = 20;
         this._timeText.x = this._timeText.width / 2;
         this._timeText.y = -this._timeText.height;
         this._arrow = new Sprite();
         this._arrow.graphics.beginFill(ControlBarConstant.TIPS_BG_COLOR,ControlBarConstant.TIPS_BG_ALPHA);
         this._arrow.graphics.lineTo(0,0);
         this._arrow.graphics.lineTo(5,5);
         this._arrow.graphics.lineTo(10,0);
         this._arrow.graphics.endFill();
         this._container.addChild(this._background);
         this._container.addChild(this._video);
         this._container.addChild(this._errorText);
         this._container.addChild(this._timeText);
         this._container.addEventListener(MouseEvent.CLICK,this.onVideoClick);
         addChild(this._arrow);
         this._errorText.visible = false;
         this._replayTimer = new Timer(PREVIEW_REPLAY_TIMEOUT);
         this._replayTimer.addEventListener(TimerEvent.TIMER,this.onReplayTimeOut);
         this._delayVideoTimer = new Timer(this._tweenSpeed * 1000,1);
         this._delayVideoTimer.addEventListener(TimerEvent.TIMER_COMPLETE,this.onDelayVideo);
      }
      
      private function onRollOut(param1:MouseEvent) : void {
         dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_UNHOVER));
      }
      
      private function onRollOver(param1:MouseEvent) : void {
         ReportUtil.sendUserActionLog(ReportUtil.PROGRESS_BAR_LITTLE_WINDOW_TO_BIG_WINDOW);
         dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_HOVER));
      }
      
      private function onMouseMove(param1:MouseEvent) : void {
      }
      
      public function setSize(param1:String, param2:Number) : void {
         switch(param1)
         {
            case ControlBarConstant.PREVIEW_MODE_SMALL:
               this._isShowPreviewVideo = true;
               this.changePreviewSize(this._textLength);
               if(this._isImagePreview)
               {
                  this._imageDisplayer.alpha = 0;
                  Tweener.to(this._imageDisplayer,0.5,{"alpha":1});
               }
               else
               {
                  this._video.setSize(VIDEO_WIDTH,VIDEO_HEIGHT);
                  if(this._videoError)
                  {
                     this._video.visible = false;
                     this._errorText.visible = true;
                  }
                  else
                  {
                     this._video.visible = true;
                     this._video.alpha = 0;
                     Tweener.to(this._video,2,{"alpha":1});
                     this._errorText.visible = false;
                  }
               }
               break;
            case ControlBarConstant.PREVIEW_MODE_TINY:
               this._isShowPreviewVideo = false;
               this.changePreviewSize(this._textLength);
               if(this._isImagePreview)
               {
                  this.hideImage();
               }
               else
               {
                  this._video.visible = false;
                  this._errorText.visible = false;
               }
               break;
         }
         this.previewMode = param1;
         this._playerWidth = param2;
         this.updateLayout(true);
      }
      
      public function attachEvent(param1:Boolean) : void {
         if(param1)
         {
            this.addEventListener(MouseEvent.ROLL_OVER,this.onRollOver);
            this.addEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
            this.addEventListener(MouseEvent.MOUSE_MOVE,this.onMouseMove);
         }
         else
         {
            this.removeEventListener(MouseEvent.ROLL_OVER,this.onRollOver);
            this.removeEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
            this.removeEventListener(MouseEvent.MOUSE_MOVE,this.onMouseMove);
         }
      }
      
      private function updateLayout(param1:Boolean = false) : void {
         if(this._isImagePreview)
         {
            if(this.x < VIDEO_WIDTH / 2)
            {
               this._imagePreviewLayer.x = -this.x + VIDEO_WIDTH / 2;
            }
            else if(this.x > this._playerWidth - VIDEO_WIDTH / 2)
            {
               this._imagePreviewLayer.x = this._playerWidth - this.x - VIDEO_WIDTH / 2;
            }
            else
            {
               this._imagePreviewLayer.x = 0;
            }
            
         }
         this._arrow.x = 0 - this._arrow.width / 2;
         this._arrow.y = -this._arrow.height - 4;
         this._container.y = this._arrow.y;
         this._container.x = this._arrow.x + this._arrow.width / 2;
         if(this.x < this._BGWidth / 2)
         {
            if(!param1)
            {
               this._container.x = -this.x + this._BGWidth / 2;
            }
            else
            {
               Tweener.to(this._container,this._tweenSpeed,{"x":-this.x + this._BGWidth / 2});
            }
         }
         if(this.x > this._playerWidth - this._BGWidth / 2)
         {
            if(!param1)
            {
               this._container.x = this._playerWidth - this.x - this._BGWidth / 2;
            }
            else
            {
               Tweener.to(this._container,this._tweenSpeed,{"x":this._playerWidth - this.x - this._BGWidth / 2});
            }
         }
      }
      
      private function changePreviewSize(param1:Number) : void {
         var _loc2_:Number = TEXT_WIDTH_MAX < param1?TEXT_WIDTH_MAX:param1;
         var _loc3_:Number = Math.ceil(param1 / TEXT_WIDTH_MAX) * 18;
         if(this._isImagePreview)
         {
            this._imageTF.alpha = 0;
            Tweener.to(this._imageTF,this._tweenSpeed,
               {
                  "alpha":1,
                  "width":_loc2_ + PADDING * 2 + 2,
                  "height":_loc3_ + 2,
                  "x":-(_loc2_ + PADDING * 2) / 2,
                  "y":-_loc3_ + this._imageBorder.y
               });
         }
         if(this._isShowPreviewVideo)
         {
            this._BGWidth = TEXT_WIDTH_MAX + PADDING * 2;
            Tweener.to(this._background,this._tweenSpeed,
               {
                  "width":this._BGWidth,
                  "height":_loc3_ + VIDEO_HEIGHT + PADDING
               });
         }
         else
         {
            this._BGWidth = _loc2_ + PADDING * 2;
            Tweener.to(this._background,this._tweenSpeed,
               {
                  "width":this._BGWidth,
                  "height":_loc3_
               });
         }
         this._timeText.alpha = 0;
         Tweener.to(this._timeText,this._tweenSpeed,
            {
               "alpha":1,
               "width":_loc2_ + PADDING * 2 + 2,
               "height":_loc3_,
               "x":-(_loc2_ + PADDING * 2) / 2,
               "y":-_loc3_
            });
         setTimeout(this.setPreviewLocal,50,this._video,_loc3_);
         this._errorText.x = -this._errorText.width / 2;
         this._errorText.y = this._video.y + (this._video.height - this._errorText.textHeight) / 2;
      }
      
      private function setPreviewLocal(param1:DisplayObject, param2:Number) : void {
         param1.alpha = 1;
         param1.x = -VIDEO_WIDTH / 2 + 1;
         param1.y = -param2 - VIDEO_HEIGHT;
      }
      
      public function close() : void {
         this.attachEvent(false);
         this._video.removeEventListener(CommonPlayerEvent.SVP_VIDEO_LOADED,this.onVideoEvent);
         this._video.removeEventListener(CommonPlayerEvent.SVP_VIDEO_END,this.onVideoEvent);
         this._video.stop();
         this._video.visible = false;
         this._videoError = false;
         this._replayTimer.stop();
         if(this._isImagePreview)
         {
            this.hideImage();
         }
      }
      
      public function setVideoURL(param1:String) : void {
         this._video.addEventListener(CommonPlayerEvent.SVP_VIDEO_LOADED,this.onVideoEvent);
         this._video.addEventListener(CommonPlayerEvent.SVP_VIDEO_END,this.onVideoEvent);
         this._video.addEventListener(CommonPlayerEvent.SVP_VIDEO_ERROR,this.onVideoEvent);
         this._videoError = false;
         this._video.visible = false;
         this._videoURL = param1;
         this._delayVideoTimer.reset();
         this._delayVideoTimer.start();
      }
      
      private function onDelayVideo(param1:TimerEvent) : void {
         this._video.videoURL = this._videoURL;
         this._video.volume = 0;
         this._video.play();
      }
      
      private function onVideoEvent(param1:CommonPlayerEvent) : void {
         switch(param1.type)
         {
            case CommonPlayerEvent.SVP_VIDEO_LOADED:
               this._video.visible = true;
               break;
            case CommonPlayerEvent.SVP_VIDEO_END:
               this._replayTimer.reset();
               this._replayTimer.start();
               break;
            case CommonPlayerEvent.SVP_VIDEO_ERROR:
               this._videoError = true;
               this._video.visible = false;
               this._errorText.visible = true;
               this.updateLayout();
               break;
         }
      }
      
      private function onReplayTimeOut(param1:TimerEvent) : void {
         this._video.seek(0);
      }
      
      private function onDisappearTimeOut(param1:TimerEvent) : void {
         dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_DISAPPEAR));
      }
      
      public function setText(param1:String) : void {
         var _loc2_:TextField = new TextField();
         _loc2_.defaultTextFormat = new TextFormat("Arial",13);
         _loc2_.width = 1000;
         _loc2_.text = param1;
         if(this._textLength != _loc2_.textWidth)
         {
            this._textLength = _loc2_.textWidth;
            this.changePreviewSize(_loc2_.textWidth);
         }
         this._timeText.text = param1;
         if(this._imageTF)
         {
            this._imageTF.text = param1;
         }
      }
      
      override public function set x(param1:Number) : void {
         super.x = param1;
         this.updateLayout();
      }
      
      public function initImagePreview(param1:Array) : void {
         this._isImagePreview = true;
         this._imagePreviewLayer = new Sprite();
         this._imagePreviewLayer.x = -VIDEO_WIDTH / 2;
         this._imagePreviewLayer.y = -9;
         this.addChild(this._imagePreviewLayer);
         this._imageDisplayer = new PreviewEffectDisplayer();
         this._imageDisplayer.init(param1);
         this._imageDisplayer.x = -VIDEO_WIDTH / 2;
         this._imageDisplayer.y = -VIDEO_HEIGHT - 18;
         this._imageDisplayer.addEventListener(PreviewerEvent.PREVIEWER_CLICK_TIME_CHANGE,this.onImageClick);
         this._imageDisplayer.addEventListener(PreviewerEvent.PREVIEWER_SHARE,this.onPreviewShare);
         this._imagePreviewLayer.addChild(this._imageDisplayer);
         this._imageBorder = AssetUtil.getAsset("C_Preview_border") as MovieClip;
         this._imageBorder.x = -int(this._imageBorder.width / 2);
         this._imageBorder.y = -this._imageBorder.height + 6;
         this._imagePreviewLayer.addChild(this._imageBorder);
         var _loc2_:TextFormat = new TextFormat();
         _loc2_.font = "Arial";
         _loc2_.size = 13;
         _loc2_.color = 16777215;
         _loc2_.leftMargin = 5;
         _loc2_.rightMargin = 5;
         _loc2_.leading = 1;
         _loc2_.align = TextFormatAlign.JUSTIFY;
         this._imageTF = new TextField();
         this._imageTF.defaultTextFormat = _loc2_;
         this._imageTF.selectable = false;
         this._imageTF.multiline = true;
         this._imageTF.wordWrap = true;
         this._imageTF.cacheAsBitmap = true;
         this._imageTF.width = 80;
         this._imageTF.height = 20;
         this._imageTF.x = -(this._imageTF.width / 2);
         this._imageTF.y = this._imageBorder.y - this._imageTF.height;
         this._imageTF.filters = [new DropShadowFilter(2,45,0,1,2,2,2)];
         this._imagePreviewLayer.addChild(this._imageTF);
      }
      
      private function onVideoClick(param1:MouseEvent) : void {
         dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_CLICK,{"time":this._jumpTime}));
         ReportUtil.sendUserActionLog(ReportUtil.PROGRESS_PREVIEW_CLICK);
      }
      
      private function onImageClick(param1:PreviewerEvent) : void {
         this._jumpTime = param1.data.time;
         dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_CLICK,{"time":this._jumpTime}));
         ReportUtil.sendUserActionLog(ReportUtil.PROGRESS_PREVIEW_CLICK);
      }
      
      private function onPreviewShare(param1:PreviewerEvent) : void {
         this.dispatchEvent(new PreviewerEvent(PreviewerEvent.PREVIEWER_SHARE,param1.data));
      }
      
      public function showImage(param1:Number) : void {
         this._imageDisplayer.showImage(param1);
         this._imagePreviewLayer.visible = true;
         this._background.visible = false;
         this._jumpTime = param1;
      }
      
      public function isCurrentImageLoaded(param1:Number) : Boolean {
         return this._imageDisplayer.isCurrentImageLoaded(param1);
      }
      
      public function hideImage() : void {
         this._imageDisplayer.hide();
         this._imagePreviewLayer.visible = false;
         this._background.visible = true;
      }
      
      public function set playerWidth(param1:Number) : void {
         this._playerWidth = param1;
      }
      
      public function get playerWidth() : Number {
         return this._playerWidth;
      }
      
      public function set isImagePreview(param1:Boolean) : void {
         this._isImagePreview = param1;
         if(param1 == false && (this._imagePreviewLayer))
         {
            this._imagePreviewLayer.visible = false;
         }
      }
   }
}
